package cn.liu.jetpack_demo.components

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.width
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.Dp
import cn.liu.jetpack_demo.R
import coil.compose.AsyncImagePainter
import coil.compose.rememberAsyncImagePainter
import coil.request.ImageRequest
import coil.size.Size

@Composable
fun LcCacheImage(url: String, width: Dp, height: Dp) {
  Box(
    modifier = Modifier
      .height(width)
      .width(width),
    contentAlignment = Alignment.Center
  ) {
    val painter = rememberAsyncImagePainter(
      model = ImageRequest.Builder(LocalContext.current)
        .data(url)
        .error(R.drawable.image_error)
        .size(Size.ORIGINAL)
        .crossfade(1000)
        .transformations()
        .build()
    )
    val paintState = painter.state
    Image(painter = painter, contentDescription = "lc Cache Image")
    if (paintState is AsyncImagePainter.State.Loading)
      CircularProgressIndicator()
  }
}
